home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 11.04 - circle at click / SimpleDraw.java < prev    next >
Text File  |  1996-04-22  |  2KB  |  64 lines

  1. /* -------------------------------------------------------------
  2. This applet paints a red circle wherever you click.
  3.  
  4. Java's classes: Applet    (applet)
  5.                 Event     (awt)     user-generated action
  6.                 Graphics  (awt)     used for drawing
  7.                 Color     (awt)     defines colors
  8.  
  9. Custom classes: SimpleDraw
  10.                 Circle              defines and draws circles
  11.  
  12. ------------------------------------------------------------- */
  13.  
  14. import java.applet.Applet;
  15. import java.awt.*;
  16.  
  17. public class SimpleDraw extends Applet {
  18.  
  19.    Circle   c;
  20.          
  21.    /** Create a circle to start with. */
  22.    public void init() {
  23.       c = new Circle();
  24.       c.initialize(50, 50);      
  25.    }
  26.    
  27.    /** Create a new red circle when the user clicks the mouse. */
  28.    public boolean mouseUp(Event e, int x, int y) {
  29.       c = new Circle();
  30.       c.initialize(x, y);
  31.       
  32.       repaint();
  33.             
  34.       return true;
  35.    }
  36.    
  37.    /** Repaint the newest circle. */
  38.    public void paint(Graphics g) {
  39.       c.draw(g);
  40.    }
  41. }
  42.  
  43. /** Maintain circle information and provide drawing capabilities. */
  44. class Circle {
  45.    Color color;
  46.    int x;
  47.    int y;
  48.    
  49.    /** Draw a circle that is 20 pixels in radius. */
  50.    void draw(Graphics g) {
  51.       g.setColor(this.color);
  52.       g.fillOval(this.x - 20, this.y - 20, 40, 40);
  53.    }
  54.    
  55.    /** Initialize a red circle at the given pixel location. */
  56.    void initialize(int x, int y) {
  57.       color = Color.red;
  58.       this.x = x;
  59.       this.y = y;           
  60.    }
  61.  
  62. }
  63.  
  64.